/*$(document).ready(function () {
    $('.summernote').summernote({
        lang: 'zh-CN'
    });
    //summernote赋值
    $('.summernote').code("123");
    //summernote取值
	var str = $('.summernote').code();
	
});*/

function initMySummernote(summernoteId) {
    $('#' + summernoteId).summernote({
        // height: 200,
        minHeight: 600,
        maxHeight: 719,
        // focus: true,
        placeholder: '添加介绍...',
        lang: 'zh-CN',
        dialogsInBody: true,
        disableDragAndDrop: false,
        toolbar: [
            ['styleTags', ['p', 'blockquote', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']],
            // [groupName, [list of button]]
            // ['style', ['bold', 'italic', 'underline', 'clear']],
            ['style', ['style','bold', 'italic', 'underline', 'clear']],
            ['font', ['strikethrough']],
            ['fontsize', ['fontsize']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['insert', ['link', 'picture']],
            ['view', ['fullscreen']]
    /*        styleTags: ['p', 'blockquote', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
        fontNames: [
        'Arial', 'Arial Black', 'Comic Sans MS', 'Courier New',
        'Helvetica Neue', 'Helvetica', 'Impact', 'Lucida Grande',
        'Tahoma', 'Times New Roman', 'Verdana'
    ],*/
        ],
        styleTags: ['p', 'blockquote', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
        popover: {
            image: [
                ['imagesize', ['imageSize100', 'imageSize50', 'imageSize25']],
                ['float', ['floatLeft', 'floatRight', 'floatNone']],
                ['remove', ['removeMedia']]
            ],
            link: [
                ['link', ['linkDialogShow', 'unlink']]
            ],
            air: [
                ['color', ['color']],
                ['font', ['bold', 'underline', 'clear']],
                ['para', ['ul', 'paragraph']],
                ['table', ['table']],
                ['insert', ['link', 'picture']]
            ]
        },
        callbacks: {
            onImageUpload: function (files, editor, $editable) {
                sendFile(files, editor, $editable, summernoteId);
            },
            onPaste: function(e) {
                // alert('Called event paste');
                console.log(e);
            }
        }
    });
}

function sendFile(files, editor, $editable, summernoteId) {
    var data = new FormData();
    for(var file of files){
        data.append("file", file);
    }
    $.ajax({
        data: data,
        type: "POST",
        url: urlcore + "/api/attachment/upload", //图片上传出来的url，返回的是图片上传后的路径，http格式
        cache: false,
        contentType: false,
        processData: false,
        dataType: "json",
        success: function (cbdata) {//data是返回的hash,key之类的值,数组，key是定义的文件名
            if(cbdata.data.length>0){
                for(var cbFile of cbdata.data)
                $('#' + summernoteId).summernote('insertImage', cbFile); // the insertImage API
            }
        },
        error: function () {
            alert("上传失败");
        }
    });
} 